package com.brtbeacon.wx.demo.mapview;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    public final static int CODE_REQUEST_PERMISSIONS = 999;

    private final static String KEY_URL = "key_url";

    private EditText editURL;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_start).setOnClickListener(this);
        editURL = findViewById(R.id.edit_url);

        checkPermissions();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_start: {
                String url = editURL.getText().toString().trim();
                if (TextUtils.isEmpty(url)) {
                    Toast.makeText(this, "请填写地图链接！", Toast.LENGTH_SHORT).show();
                    break;
                }
                MapWebViewActivity.startActivity(this, url);
                break;
            }
        }
    }

    private void checkPermissions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, CODE_REQUEST_PERMISSIONS);
            }
        }
    }

}
